#include "TRSTransform.h"
#include "math.h"
TRSTransform::TRSTransform(void):m_Translation(0,0,0),m_Rotation(0,0,0),m_Scale(1,1,1)
{

}

TRSTransform::~TRSTransform(void)
{
}

void TRSTransform::ResetTransformParameters()
{
	m_Translation.SetValues(0,0,0);
	m_Scale.SetValues(1,1,1);
	m_Rotation.SetValues(0,0,0);
	m_Changed = true;
}

void TRSTransform::RecalculateMatrix() const
{
	m_ScaleMatrix.SetCell(0,0,m_Scale.x);
	m_ScaleMatrix.SetCell(1,1,m_Scale.y);
	m_ScaleMatrix.SetCell(2,2,m_Scale.z);
	// x rotation
	float cosx = cos (m_Rotation.x);
	float sinx = sin (m_Rotation.x);
	m_RotationXMatrix.SetAsIdentity();
	m_RotationXMatrix.SetCell(1,1,cosx);
	m_RotationXMatrix.SetCell(1,2,sinx);
	m_RotationXMatrix.SetCell(2,1,-sinx);
	m_RotationXMatrix.SetCell(2,2,cosx);
	
	// y rotation
	float cosy = cos (m_Rotation.y);
	float siny = sin (m_Rotation.y);
	m_RotationYMatrix.SetAsIdentity();
	m_RotationYMatrix.SetCell(0,0,cosy);
	m_RotationYMatrix.SetCell(0,2,-siny);
	m_RotationYMatrix.SetCell(2,0,+siny);
	m_RotationYMatrix.SetCell(2,2,cosy);
	// z rotation.
	float cosz = cos (m_Rotation.z);
	float sinz = sin (m_Rotation.z);
	m_RotationZMatrix.SetAsIdentity();
	m_RotationZMatrix.SetCell(0,0, cosz);
	m_RotationZMatrix.SetCell(0,1, +sinz);
	m_RotationZMatrix.SetCell(1,0, -sinz);
	m_RotationZMatrix.SetCell(1,1, cosz);
	// translation
	m_TranslationMatrix.SetAsIdentity();
	m_TranslationMatrix.SetRow(3,m_Translation.x,m_Translation.y,m_Translation.z,1);
	
	// multiply first 2 matrices and store into compiledmatrix.
	m_ScaleMatrix.Multiply(&m_CompiledMatrix,&m_RotationXMatrix);
	m_CompiledMatrix.Multiply(&m_CompiledMatrix,&m_RotationYMatrix);
	m_CompiledMatrix.Multiply(&m_CompiledMatrix,&m_RotationZMatrix);
	m_CompiledMatrix.Multiply(&m_CompiledMatrix,&m_TranslationMatrix);
}